﻿/**
 * Copyright © TnT-VNGroup 2011
 * PROJECT:   SmartCollector
 * $HeadURL: http://smartcollector.googlecode.com/svn/branches/Srcs/SmartCollector.Domain/Repository/SiteRepository.cs $
 * $Author: chithanh12@gmail.com $
 * $Date: 2011-04-27 10:39:20 +0000 (Wed, 27 Apr 2011) $
 * $Rev: 100 $
 **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Configuration;
using SmartCollector.Domain;
using SmartCollector.Domain.Entity;
namespace SmartCollector.Domain.Repository
{
    public class SiteRepository
    {
        private Table<tbl_ReferenceSite> m_sites;

        public IQueryable<tbl_ReferenceSite> Sites
        {
            get
            {
                return m_sites.AsQueryable();
            }
        }

        public SiteRepository()
        {
            m_sites = (new EntityDomainDataContext()).GetTable<tbl_ReferenceSite>();
        }

        public void Refresh()
        {
            m_sites = (new EntityDomainDataContext()).GetTable<tbl_ReferenceSite>();
        }

        public void AddNewSites(tbl_ReferenceSite site)
        {
            m_sites.InsertOnSubmit(site);
        }

        public void DeleteSite(tbl_ReferenceSite site)
        {
            m_sites.DeleteOnSubmit(site);
        }

        public void DeleteSiteById(int id)
        {
            tbl_ReferenceSite site = m_sites.Where(c => c.ID == id).First();
            if (site != null)
            {
                m_sites.DeleteOnSubmit(site);
            }
        }

        public void SubmitChanges()
        {
            m_sites.Context.SubmitChanges();
        }

        public string GetSiteNameById(int id)
        {
            return m_sites.Where(p => p.ID == id)
                .Select(c => c.SiteName).FirstOrDefault();
        }

    }
}
